package com.ewanghuiju.app.presenter.pintuan;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.pintuan.PintuanShopDetailsContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanGoodsSpecsResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanOnlineListResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.model.bean.response.ShareResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.exception.ApiException;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/16
 */
public class PintuanShopDetailsPresenter extends RxPresenter<PintuanShopDetailsContract.View> implements PintuanShopDetailsContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public PintuanShopDetailsPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData() {

    }

    @Override
    public void attachView(PintuanShopDetailsContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 刷新商品信息
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_GET_SHOP_INFO;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        if (mView != null) {
                            mView.refreshPtigroupList();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }

    /**
     * 获取商品详情
     *
     * @param goodsid
     */
    @Override
    public void getShopInfo(String goodsid) {
        addSubscribe(mDataManager.getptigoodsInfo(goodsid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<PintuanShopListResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<PintuanShopListResponBean> optional) {
                        if (mView != null)
                            mView.showShopInfo(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 获取商品规格
     *
     * @param goodsid
     */
    @Override
    public void getGoodsspecsData(String goodsid) {
        addSubscribe(mDataManager.getPtigoodsSpecs(goodsid)
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<List<PintuanGoodsSpecsResponBean>>>(mView) {
                            @Override
                            public void onNext(Optional<List<PintuanGoodsSpecsResponBean>> optional) {
                                if (mView != null)
                                    mView.showGoodsspecsData(optional.getIncludeNull());
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                                if(mView!=null)
//                                    mView.showContentError();
                            }
                        })
        );
    }

    /**
     * 检测团长身份
     *
     * @param goodsid
     */
    @Override
    public void ptigetIdentity(String goodsid) {
        addSubscribe(mDataManager.ptigetIdentity(goodsid)
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView, true, false) {
                            @Override
                            public void onNext(Optional<BaseResponse> optional) {
                                if (mView != null)
                                    mView.showPtigetIdentitySuccess();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                                0=可以开团,1=不是团长身份,2=商品id为空,3=商品不存在,4=开团额度不足,5=余额不足,
                                if (mView != null) {
                                    if (e instanceof ApiException) {
                                        mView.showPtigetIdentityFail(((ApiException) e).getCode(), e.getMessage());
                                    }else{
                                        mView.showPtigetIdentityFail(100, "网络加载失败");
                                    }
                                }
                            }
                        })
        );

    }

    /**
     * 确认订单
     *
     * @param goodsid
     * @param specsid
     */
    @Override
    public void pticonfirmationOrder(String goodsid, String specsid, String group_id) {
        addSubscribe(mDataManager.pticonfirmationOrder(goodsid, specsid, group_id)
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<AddressListResponBean>>(mView) {
                            @Override
                            public void onNext(Optional<AddressListResponBean> optional) {
                                if (mView != null)
                                    mView.showPticonfirmationOrder(optional.getIncludeNull(), specsid);
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                        if(mView!=null){
//                            if (e instanceof ApiException) {
//                                mView.showPtigetIdentityFail(((ApiException) e).getCode(),e.getMessage());
//                            }
//                        }
                            }
                        })
        );

    }

    /**
     * 获取团列表
     *
     * @param goodsid
     */
    @Override
    public void getPtigroupList(String goodsid) {
        addSubscribe(mDataManager.getPtigroupList(goodsid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<PintuanOnlineListResponBean>>>(mView, true, false) {
                    @Override
                    public void onNext(Optional<List<PintuanOnlineListResponBean>> optional) {
                        if (mView != null)
                            mView.showPtigroupList(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null)
                            mView.showPtigroupListError();
                    }
                })
        );
    }

    /**
     * 获取分享信息
     *
     * @param goodsid
     * @param groupid
     */
    @Override
    public void getPtiptGoodsShare(String goodsid, String groupid) {
        addSubscribe(mDataManager.ptiptGoodsShare(goodsid, groupid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<ShareResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<ShareResponBean> optional) {
                        if (mView != null)
                            mView.showPtiptGoodsShare(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 检测是否可以开团
     *
     * @param groupid
     */
    @Override
    public void pticheckGroup(String groupid) {
        addSubscribe(mDataManager.pticheckGroup(groupid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.pticheckGroupSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }


}
